package org.benjamin.lending.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.benjamin.lending.domain.entity.ResourceData;
import org.benjamin.lending.repository.ResourceDataRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping(value = "uploader")
public class UploaderController {
	
	private Logger logger = LogManager.getLogger();
	
	@Autowired
	private ResourceDataRepository resourceDataRepository;
	
	@ResponseBody
	@PostMapping(value = "image")
	public Map<String,String> upImage(MultipartFile multipartFile,boolean dataType,String imageInfo) throws IOException {
		logger.info("任务上传中");
		if(multipartFile.isEmpty()) {
			logger.warn("文件上传数据不存在的");
		}
		ResourceData resourceData = new ResourceData();
		byte[] data = multipartFile.getBytes();
		resourceData.setContentData(data);
		resourceData.setContentType(multipartFile.getContentType());
		resourceData.setContentName(multipartFile.getOriginalFilename());
		resourceData.setInfo(imageInfo);
		this.resourceDataRepository.save(resourceData);
		Map<String,String> returnMap = new HashMap<String,String>();
		returnMap.put("id", resourceData.getId().toString());
		return returnMap;
	}

}
